package com.toutoushidao.ssm.service;

import com.alibaba.fastjson.JSONArray;
import com.mongodb.BasicDBObject;
import com.toutoushidao.ssm.dao.MongoDbDao;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OngService {

    @Autowired
    public MongoDbDao dbDao;

    private final Logger LOG = LoggerFactory.getLogger(this.getClass());

    /**
     * 用户概要信息列表
     *
     * @return
     */
    public JSONArray userInfo() {
        Map<String, Object> gameResult = new HashMap<>();
        BasicDBObject projectionDoc = new BasicDBObject();
        projectionDoc.put("_id", 1);
        projectionDoc.put("uid", 1);
        projectionDoc.put("coinCnt", 1);
        projectionDoc.put("ongCoin", 1);
        projectionDoc.put("maxScore", 1);
        projectionDoc.put("reliveCard", 1);
        projectionDoc.put("boxHis", 1);
        projectionDoc.put("drawHis", 1);
        List<Document> userList = dbDao.selectSortPage("user", new BasicDBObject(), projectionDoc, new BasicDBObject());

        for (Document user : userList) {
            String _id = user.get("_id").toString();
            long createTime = Integer.parseInt(_id.substring(0, 8), 16);
//                amtSum = amtSum.add(new BigDecimal(ongCoin));
            SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
            String createDate = sdf.format(new Date(createTime * 1000));
            user.put("_id", createDate);
            LOG.debug(">>>>>>>>>>>> 用户概要信息 " + user.toString());
        }
        JSONArray jsonArray = (JSONArray) JSONArray.toJSON(userList);
        gameResult.put("userList", userList);
        gameResult.put("userCnt", userList.size());
        return jsonArray;
    }

    /**
     * 用户交易信息列表
     *
     * @return
     */
    public JSONArray tradeInfo() {
        Map<String, Object> gameResult = new HashMap<>();
        BasicDBObject projectionDoc = new BasicDBObject();
        projectionDoc.put("_id", 1);
        projectionDoc.put("uid", 1);
        projectionDoc.put("type", 1);
        projectionDoc.put("hash", 1);
        projectionDoc.put("ip", 1);
        projectionDoc.put("time", 1);
        List<Document> tradeList = dbDao.selectSortPage("trade", new BasicDBObject(), projectionDoc,
                new BasicDBObject("time", -1), 0, 100);

        for (Document trade : tradeList) {
            String _id = trade.get("_id").toString();
            long createTime = Integer.parseInt(_id.substring(0, 8), 16);
            SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
            String createDate = sdf.format(new Date(createTime * 1000));
            trade.put("_id", createDate);
            LOG.debug(">>>>>>>>>>>> 用户交易概要信息 " + trade.toString());
        }
        JSONArray jsonArray = (JSONArray) JSONArray.toJSON(tradeList);
        gameResult.put("tradeList", tradeList);
        return jsonArray;
    }
}
